﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Text;

namespace SqlToolkit.Oracle.Schema
{
    public class SqlDbTableFieldStatistics : SqlToolkit.ISqlDataObject
    {

        string _tableName = string.Empty;
        string _columnName = string.Empty;
        string _numDistinct = string.Empty;
        string _lowValue = string.Empty;
        string _highValue = string.Empty;
        string _density = string.Empty;
        string _numNulls = string.Empty;
        string _numBuckets = string.Empty;
        string _lastAnalyzed = string.Empty;
        string _sampleSize = string.Empty;
        string _globalStats = string.Empty;
        string _userStats = string.Empty;
        string _avgColLen = string.Empty;

        public string TableName
        {
            get { return _tableName; }
            set { _tableName = value; }
        }
        public string ColumnName
        {
            get { return _columnName; }
            set { _columnName = value; }
        }
        public string NumDistinct
        {
            get { return _numDistinct; }
            set { _numDistinct = value; }
        }
        public string LowValue
        {
            get { return _lowValue; }
            set { _lowValue = value; }
        }
        public string HighValue
        {
            get { return _highValue; }
            set { _highValue = value; }
        }
        public string Density
        {
            get { return _density; }
            set { _density = value; }
        }
        public string NumNulls
        {
            get { return _numNulls; }
            set { _numNulls = value; }
        }
        public string NumBuckets
        {
            get { return _numBuckets; }
            set { _numBuckets = value; }
        }
        public string LastAnalyzed
        {
            get { return _lastAnalyzed; }
            set { _lastAnalyzed = value; }
        }
        public string SampleSize
        {
            get { return _sampleSize; }
            set { _sampleSize = value; }
        }
        public string GlobalStats
        {
            get { return _globalStats; }
            set { _globalStats = value; }
        }
        public string UserStats
        {
            get { return _userStats; }
            set { _userStats = value; }
        }
        public string AvgColLen
        {
            get { return _avgColLen; }
            set { _avgColLen = value; }
        }


        public static SqlDbTableFieldStatistics Build(IDataReader r)
        {
            if(null == r) return null;

            SqlDbTableFieldStatistics ret = new SqlDbTableFieldStatistics();

            System.Text.Encoding e = System.Text.Encoding.GetEncoding("ISO-8859-1");

            ret._tableName =    (r.IsDBNull(0)) ? string.Empty : Convert.ToString(r.GetValue(0));
            ret._columnName =   (r.IsDBNull(1)) ? string.Empty : Convert.ToString(r.GetValue(1));
            ret._numDistinct =  (r.IsDBNull(2)) ? string.Empty : Convert.ToString(r.GetValue(2));

            ret._lowValue =     (r.IsDBNull(3)) ? string.Empty : e.GetString((byte[])r.GetValue(3));
            ret._highValue =    (r.IsDBNull(4)) ? string.Empty : e.GetString((byte[])r.GetValue(4));
            
            ret._density =      (r.IsDBNull(5)) ? string.Empty : Convert.ToString(r.GetValue(5));
            ret._numNulls =     (r.IsDBNull(6)) ? string.Empty : Convert.ToString(r.GetValue(6));
            ret._numBuckets =   (r.IsDBNull(7)) ? string.Empty : Convert.ToString(r.GetValue(7));
            ret._lastAnalyzed = (r.IsDBNull(8)) ? string.Empty : Convert.ToString(r.GetValue(8));
            ret._sampleSize =   (r.IsDBNull(9)) ? string.Empty : Convert.ToString(r.GetValue(9));
            ret._globalStats =  (r.IsDBNull(10)) ? string.Empty : Convert.ToString(r.GetValue(10));
            ret._userStats =    (r.IsDBNull(11)) ? string.Empty : Convert.ToString(r.GetValue(11));
            ret._avgColLen =    (r.IsDBNull(12)) ? string.Empty : Convert.ToString(r.GetValue(12));

            e = null;

            return ret;
        }

        public static SqlDbTableFieldStatisticsCollection BuildCollection(IDataReader r)
        {
            if (null == r) return null;

            SqlDbTableFieldStatisticsCollection ret = new SqlDbTableFieldStatisticsCollection();
            while (r.Read())
            {
                SqlDbTableFieldStatistics tmp = Build(r);
                if (null != tmp)
                {
                    ret.Add(tmp);
                }
            }

            if (ret.Count < 1) ret = null;

            return ret;
        }
    }
}
